package com.ls.hnks.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.hnks.util.Result;
import com.ls.hnks.util.ResultCodeEnum;
import com.ls.hnks.mapper.CustomerMapper;
import com.ls.hnks.model.Customer;
import com.ls.hnks.model.CustomerAttachment;
import com.ls.hnks.model.CustomerPic;
import com.ls.hnks.model.CustomerReservedContact;
import com.ls.hnks.model.vo.CustomerDetailsVo;
import com.ls.hnks.service.CustomerAttachmentService;
import com.ls.hnks.service.CustomerPicService;
import com.ls.hnks.service.CustomerReservedContactService;
import com.ls.hnks.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 15375
* @description 针对表【tb_customer(客户信息)】的数据库操作Service实现
* @createDate 2023-01-25 21:56:37
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Autowired
    private CustomerPicService picService;

    @Autowired
    private CustomerReservedContactService reservedContactService;

    @Autowired
    private CustomerAttachmentService attachmentService;

    @Override
    public Result getDetails(String id) {
        Customer customer = this.getById(id);
        if(customer == null){
            return Result.error(ResultCodeEnum.CUSTOMER_NOT_FOUND);
        }
        CustomerDetailsVo customerDetailsVo = new CustomerDetailsVo();
        BeanUtils.copyProperties(customer,customerDetailsVo);

        // 查找顾客的联系信息
        List<CustomerReservedContact> contractsByCustomerId = reservedContactService.getContractsByCustomerId(id);
        if(contractsByCustomerId.size() > 0) {
            customerDetailsVo.setCustomerReservedContactList(contractsByCustomerId);
        }

        // 附件信息
        List<CustomerAttachment> attachmentsByCustomerId = attachmentService.getAttachmentsByCustomerId(id);
        if(attachmentsByCustomerId.size() > 0) {
            customerDetailsVo.setAttachmentList(attachmentsByCustomerId);
        }

        return Result.ok(customerDetailsVo);
    }

    @Override
    public Result getCustomerByPage(Integer pageNo, Integer size) {
        Page<Customer> page = new Page<>(pageNo,size);
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.orderByAsc("num");
        IPage<Customer> customerPage = this.page(page, customerQueryWrapper);
        List<Customer> records = customerPage.getRecords();
        if(records != null && records.size() > 0) {
            List<Integer> ids = records.stream().map(Customer::getId).collect(Collectors.toList());
            List<CustomerPic> customerPicList = picService.getPicsByCustomerIds(ids);
            for (Customer customer : records) {
                for (CustomerPic pic : customerPicList) {
                    if (Objects.equals(customer.getId(), pic.getCustomerid()))
                        customer.setAvatar(pic.getUrl());
                }
            }
        }
        return Result.ok(customerPage);
    }

    @Transactional
    @Override
    public void saveCustomer(Customer customer) {
        int createUserId = Integer.parseInt(StpUtil.getLoginId().toString());
        int count = this.count();
        customer.setNum(count+1);
        customer.setCreateuserid(createUserId);
        this.save(customer);


        String avatar = customer.getAvatar();
        if(!StringUtils.isEmpty(avatar)){
            CustomerPic customerPic = new CustomerPic();
            customerPic.setUrl(avatar);
            customerPic.setCustomerid(customer.getId());
            customerPic.setCreateeuserid(createUserId);
            picService.save(customerPic);
        }

        List<CustomerReservedContact> customerReservedContactList = customer.getCustomerReservedContactList();
        if(customerReservedContactList != null && customerReservedContactList.size() > 0) {
            for (CustomerReservedContact item : customerReservedContactList) {
                item.setCustomerid(customer.getId());
                item.setCreateuserid(createUserId);
            }
            reservedContactService.saveBatch(customerReservedContactList);
        }

        List<CustomerAttachment> attachmentList = customer.getAttachmentList();
        if (attachmentList != null && attachmentList.size() > 0) {
            for (CustomerAttachment item : attachmentList) {
                item.setServicetemplateitemid(customer.getId());
                item.setCreateuserid(createUserId);
            }
            attachmentService.saveBatch(attachmentList);
        }
    }

    @Override
    @Transactional
    public Result removeCustomer(String id) {
        Customer customer = this.getById(id);
        if(customer == null){
            return Result.error(ResultCodeEnum.CUSTOMER_NOT_FOUND);
        }
        this.removeById(id);
        return Result.ok();
    }
}




